package com.mrdeer.web.controller.sys;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mrdeer.common.JSONResult;
import com.mrdeer.common.entity.dto.DictDataDTO;
import com.mrdeer.common.entity.sys.Dict;
import com.mrdeer.common.entity.sys.DictData;
import com.mrdeer.common.tools.RedisTemplateHelper;
import com.mrdeer.web.service.sys.SysDictDataService;
import com.mrdeer.web.service.sys.SysDictService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@RestController
@Api(tags = "字典数据管理接口")
@RequestMapping("/mrdeer/dictData")
@Transactional
public class SysDictDataController {

    @Resource
    private SysDictService sysDictService;

    @Resource
    private SysDictDataService sysDictDataService;

    @Resource
    private RedisTemplateHelper redisTemplate;

    @RequestMapping(value = "/getByCondition", method = RequestMethod.POST)
    @ApiOperation(value = "多条件分页获取字典数据列表")
    public JSONResult getByCondition(@RequestBody(required = false) DictDataDTO dictDataDTO) {
        try {
            Page<DictData> page = sysDictDataService.list(dictDataDTO);
            if (null != page) {
                return JSONResult.ok(page);
            }
        } catch (Exception e) {
            return JSONResult.errorMsg(e.getMessage());
        }
        return JSONResult.errorMsg("");
    }

    @RequestMapping(value = "/getByType/{type}", method = RequestMethod.GET)
    @ApiOperation(value = "通过类型获取")
    @Cacheable(key = "#type")
    public JSONResult getType(@PathVariable String type) {
        Dict dict = sysDictService.getByType(type);
        if (dict == null) {
            return JSONResult.errorMsg("字典类型" + type + " 不存在");
        }
        List<DictData> list = sysDictDataService.getByDictId(dict.getId());
        return JSONResult.ok(list);
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ApiOperation(value = "添加")
    public JSONResult add(@RequestBody DictDataDTO dto) {
        Dict dict = sysDictService.getById(dto.getDictId());
        if (dict == null) {
            return JSONResult.errorMsg("字典类型id不存在");
        }
        sysDictDataService.add(dto);
        //删除缓存
        redisTemplate.delete("dto::" + dict.getType());
        return JSONResult.ok("添加成功");
    }

    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    @ApiOperation(value = "编辑")
    public JSONResult edit(@RequestBody DictData dictData) {
        sysDictDataService.update(dictData);
        redisTemplate.deleteByPattern("dictData::*");
        return JSONResult.ok("编辑成功");
    }

    @RequestMapping(value = "/delByIds", method = RequestMethod.POST)
    @ApiOperation(value = "批量通过id删除")
    public JSONResult delByIds(@RequestParam List<String> ids) {
        sysDictDataService.removeByIds(ids);
        redisTemplate.delete("dictData::*");
        return JSONResult.ok("批量通过id删除数据成功");
    }
}
